const request = require('../../utils/request');

Page({
  data: {
    orders: null
  },

  onLoad() {
    this.getOrders();
  },

  onShow() {
    // 每次显示页面时刷新订单数据
    this.getOrders();
  },

  onPullDownRefresh() {
    // 下拉刷新
    this.getOrders().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  async getOrders() {
    try {
      wx.showLoading({
        title: '加载中...'
      });

      const res = await request('/api/order/stats', {
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        }
      });
      
      console.log('订单统计:', res);
      this.setData({
        orders: res
      });
    } catch (error) {
      console.error('获取订单失败:', error);
      wx.showToast({
        title: '获取订单失败',
        icon: 'none'
      });
    } finally {
      wx.hideLoading();
    }
  }
});